package com.woniu.adopt.web;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt.result.ResultResponse;
import com.woniu.adopt.service.LogLoginService;
import com.woniu.adopt.vo.UserLogLoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("log")
@Api(description = "登录日志接口")
public class LogLoginController {
    @Autowired
    LogLoginService logLoginService;

    @ApiOperation(value = "前台用户登录日志列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "now", value = "查询的当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "size", value = "每页显示多少条", required = true, defaultValue = "9"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "前台用户的用户名", required = false)
    })
    @GetMapping("/userLoginList")
    public ResultResponse<PageInfo<UserLogLoginVo>> userLoginList(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                                  @RequestParam(value = "size", defaultValue = "9") Integer size,
                                                                  String name) {
        ResultResponse<PageInfo<UserLogLoginVo>> result = null;

        try {
            PageInfo<UserLogLoginVo> rolePageInfo = logLoginService.queryUserByPage(now, size, name);
            result = new ResultResponse<>(200, "查询成功!", rolePageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询失败!");
        }
        return result;
    }

    /**
     * 文件导出
     * @return
     */
    @GetMapping("/exportExcel")
    public void exportExcel() throws IOException {
        //获取数据
        List<UserLogLoginVo>list= logLoginService.queryAll();
        //导出excel
        //表格名设置
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("前台用户登录日志列表", "前台用户登录日志"), UserLogLoginVo.class, list);
//        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("前台用户登录日志.xls","UTF-8"));
        //导出到桌面
//        ServletOutputStream outputStream = response.getOutputStream();
        FileOutputStream outputStream = new FileOutputStream("G:/app/登录日志.xls");
        workbook.write(outputStream);
        //关流"G:/app/"
        outputStream.close();
        workbook.close();
    }

}
